using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VVTGBST
    {
        public DataRow Convert2VTGBST(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryVTGBST(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["VTGMANDANT"] = " ".PadRight(2 ); 
         dwReturn["VTGSA"] = " ".PadRight(2 ); 
         dwReturn["VTGTPMA"] = " ".PadRight(4 ); 
         dwReturn["VTGKUNDNRE"] = " ".PadRight(8 ); 
         dwReturn["VTGKUNDNRB"] = " ".PadRight(8 ); 
         dwReturn["VTGMJJ"] = " ".PadRight(4 ); 
         dwReturn["VTGMM"] = " ".PadRight(2 ); 
         dwReturn["VTGME01"] = " ".PadRight(0 ); 
         dwReturn["VTGME20"] = " ".PadRight(0 ); 
         dwReturn["VTGME21"] = " ".PadRight(0 ); 
         dwReturn["VTGME31"] = " ".PadRight(0 ); 
         dwReturn["VTGME42"] = " ".PadRight(0 ); 
         dwReturn["VTGME43"] = " ".PadRight(0 ); 
         dwReturn["VTGMER01"] = " ".PadRight(0 ); 
         dwReturn["VTGMER02"] = " ".PadRight(0 ); 
         dwReturn["VTGMER03"] = " ".PadRight(0 ); 
         dwReturn["VTGMER04"] = " ".PadRight(0 ); 
         dwReturn["VTGBSTG"] = " ".PadRight(0 ); 
         dwReturn["VTGME21VOR"] = " ".PadRight(0 ); 
         dwReturn["VTGBSTAN"] = " ".PadRight(0 ); 
         dwReturn["VTGBSTMA"] = " ".PadRight(0 ); 
         dwReturn["VTGBSTA"] = " ".PadRight(0 ); 
         dwReturn["VTGBSTME"] = " ".PadRight(0 ); 
         dwReturn["VTGINDAT"] = " ".PadRight(0 ); 
         dwReturn["VTGINBST"] = " ".PadRight(0 ); 
         dwReturn["VTGINALT"] = " ".PadRight(0 ); 
         dwReturn["VTGEKPRMA"] = " ".PadRight(0 ); 
         dwReturn["VTGEKPRME"] = " ".PadRight(0 ); 
         dwReturn["VTGEKPRZU"] = " ".PadRight(0 ); 
         dwReturn["VTGVRPR"] = " ".PadRight(0 ); 
         dwReturn["VTGDLABST"] = " ".PadRight(0 ); 
         dwReturn["VTGDLATAG"] = " ".PadRight(2 ); 
         dwReturn["VTGMINBST"] = " ".PadRight(0 ); 
         dwReturn["VTGMAXBST"] = " ".PadRight(0 ); 
         dwReturn["VTGUEBER"] = " ".PadRight(1 ); 
         dwReturn["VTGAENDZL"] = " ".PadRight(0 ); 
         dwReturn["VTGFILLER"] = " ".PadRight(27 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapVTGBST(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveVTGBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryVTGBST(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapVTGBST(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertVTGBST(dwLocal, ITXDBVERSION);
            }
            UpdateVTGBST(dwLocal);
        }
        public void InsertVTGBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO VTGBST(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryVTGBST(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM VTGBST WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM VTGBST  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "VTGBST";
            Return = Result;
            return Return;
        }
        public void DeleteVTGBST(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM VTGBST   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateVTGBST(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE VTGBST SET "
                    +"VTGMANDANT= :VTGMANDANT,"
                    +"VTGSA= :VTGSA,"
                    +"VTGTPMA= :VTGTPMA,"
                    +"VTGKUNDNRE= :VTGKUNDNRE,"
                    +"VTGKUNDNRB= :VTGKUNDNRB,"
                    +"VTGMJJ= :VTGMJJ,"
                    +"VTGMM= :VTGMM,"
                    +"VTGME01= :VTGME01,"
                    +"VTGME20= :VTGME20,"
                    +"VTGME21= :VTGME21,"
                    +"VTGME31= :VTGME31,"
                    +"VTGME42= :VTGME42,"
                    +"VTGME43= :VTGME43,"
                    +"VTGMER01= :VTGMER01,"
                    +"VTGMER02= :VTGMER02,"
                    +"VTGMER03= :VTGMER03,"
                    +"VTGMER04= :VTGMER04,"
                    +"VTGBSTG= :VTGBSTG,"
                    +"VTGME21VOR= :VTGME21VOR,"
                    +"VTGBSTAN= :VTGBSTAN,"
                    +"VTGBSTMA= :VTGBSTMA,"
                    +"VTGBSTA= :VTGBSTA,"
                    +"VTGBSTME= :VTGBSTME,"
                    +"VTGINDAT= :VTGINDAT,"
                    +"VTGINBST= :VTGINBST,"
                    +"VTGINALT= :VTGINALT,"
                    +"VTGEKPRMA= :VTGEKPRMA,"
                    +"VTGEKPRME= :VTGEKPRME,"
                    +"VTGEKPRZU= :VTGEKPRZU,"
                    +"VTGVRPR= :VTGVRPR,"
                    +"VTGDLABST= :VTGDLABST,"
                    +"VTGDLATAG= :VTGDLATAG,"
                    +"VTGMINBST= :VTGMINBST,"
                    +"VTGMAXBST= :VTGMAXBST,"
                    +"VTGUEBER= :VTGUEBER,"
                    +"VTGAENDZL= :VTGAENDZL,"
                    +"VTGFILLER= :VTGFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("VTGMANDANT", OracleType.NVarChar).Value = dwData["VTGMANDANT"].ToString(); 
          or1.Parameters.Add("VTGSA", OracleType.NVarChar).Value = dwData["VTGSA"].ToString(); 
          or1.Parameters.Add("VTGTPMA", OracleType.NVarChar).Value = dwData["VTGTPMA"].ToString(); 
          or1.Parameters.Add("VTGKUNDNRE", OracleType.NVarChar).Value = dwData["VTGKUNDNRE"].ToString(); 
          or1.Parameters.Add("VTGKUNDNRB", OracleType.NVarChar).Value = dwData["VTGKUNDNRB"].ToString(); 
          or1.Parameters.Add("VTGMJJ", OracleType.NVarChar).Value = dwData["VTGMJJ"].ToString(); 
          or1.Parameters.Add("VTGMM", OracleType.NVarChar).Value = dwData["VTGMM"].ToString(); 
          or1.Parameters.Add("VTGME01", OracleType.NVarChar).Value = dwData["VTGME01"].ToString(); 
          or1.Parameters.Add("VTGME20", OracleType.NVarChar).Value = dwData["VTGME20"].ToString(); 
          or1.Parameters.Add("VTGME21", OracleType.NVarChar).Value = dwData["VTGME21"].ToString(); 
          or1.Parameters.Add("VTGME31", OracleType.NVarChar).Value = dwData["VTGME31"].ToString(); 
          or1.Parameters.Add("VTGME42", OracleType.NVarChar).Value = dwData["VTGME42"].ToString(); 
          or1.Parameters.Add("VTGME43", OracleType.NVarChar).Value = dwData["VTGME43"].ToString(); 
          or1.Parameters.Add("VTGMER01", OracleType.NVarChar).Value = dwData["VTGMER01"].ToString(); 
          or1.Parameters.Add("VTGMER02", OracleType.NVarChar).Value = dwData["VTGMER02"].ToString(); 
          or1.Parameters.Add("VTGMER03", OracleType.NVarChar).Value = dwData["VTGMER03"].ToString(); 
          or1.Parameters.Add("VTGMER04", OracleType.NVarChar).Value = dwData["VTGMER04"].ToString(); 
          or1.Parameters.Add("VTGBSTG", OracleType.NVarChar).Value = dwData["VTGBSTG"].ToString(); 
          or1.Parameters.Add("VTGME21VOR", OracleType.NVarChar).Value = dwData["VTGME21VOR"].ToString(); 
          or1.Parameters.Add("VTGBSTAN", OracleType.NVarChar).Value = dwData["VTGBSTAN"].ToString(); 
          or1.Parameters.Add("VTGBSTMA", OracleType.NVarChar).Value = dwData["VTGBSTMA"].ToString(); 
          or1.Parameters.Add("VTGBSTA", OracleType.NVarChar).Value = dwData["VTGBSTA"].ToString(); 
          or1.Parameters.Add("VTGBSTME", OracleType.NVarChar).Value = dwData["VTGBSTME"].ToString(); 
          or1.Parameters.Add("VTGINDAT", OracleType.NVarChar).Value = dwData["VTGINDAT"].ToString(); 
          or1.Parameters.Add("VTGINBST", OracleType.NVarChar).Value = dwData["VTGINBST"].ToString(); 
          or1.Parameters.Add("VTGINALT", OracleType.NVarChar).Value = dwData["VTGINALT"].ToString(); 
          or1.Parameters.Add("VTGEKPRMA", OracleType.NVarChar).Value = dwData["VTGEKPRMA"].ToString(); 
          or1.Parameters.Add("VTGEKPRME", OracleType.NVarChar).Value = dwData["VTGEKPRME"].ToString(); 
          or1.Parameters.Add("VTGEKPRZU", OracleType.NVarChar).Value = dwData["VTGEKPRZU"].ToString(); 
          or1.Parameters.Add("VTGVRPR", OracleType.NVarChar).Value = dwData["VTGVRPR"].ToString(); 
          or1.Parameters.Add("VTGDLABST", OracleType.NVarChar).Value = dwData["VTGDLABST"].ToString(); 
          or1.Parameters.Add("VTGDLATAG", OracleType.NVarChar).Value = dwData["VTGDLATAG"].ToString(); 
          or1.Parameters.Add("VTGMINBST", OracleType.NVarChar).Value = dwData["VTGMINBST"].ToString(); 
          or1.Parameters.Add("VTGMAXBST", OracleType.NVarChar).Value = dwData["VTGMAXBST"].ToString(); 
          or1.Parameters.Add("VTGUEBER", OracleType.NVarChar).Value = dwData["VTGUEBER"].ToString(); 
          or1.Parameters.Add("VTGAENDZL", OracleType.NVarChar).Value = dwData["VTGAENDZL"].ToString(); 
          or1.Parameters.Add("VTGFILLER", OracleType.NVarChar).Value = dwData["VTGFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
